package cn.wolfcode.web.controller;

import cn.wolfcode.domain.SysDictData;
import cn.wolfcode.domain.SysDictType;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SysDictDataQueryObject;
import cn.wolfcode.service.ISysDictDataService;
import cn.wolfcode.service.ISysDictTypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * ClassName SysDictDataController
 * Create by Mike
 * Date 2021/8/1 16:52
 */
@Controller
@RequestMapping("/sysDictData")
public class SysDictDataController {

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo")SysDictDataQueryObject qo){
        PageInfo<SysDictData> pageInfo = sysDictDataService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/sysDictData/list";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SysDictData sysDictData){
        if (sysDictData.getId() == null){
            sysDictDataService.save(sysDictData);
        }else {
            sysDictDataService.update(sysDictData);
        }
        return "redirect:/sysDictData/list?type=" + sysDictData.getType();
    }

    @RequestMapping("/input")
    public String input(Long id,Model model,String type){

        List<SysDictType> sysDictTypes = sysDictTypeService.listAll();
        model.addAttribute("sysDictTypes",sysDictTypes);

        if (StringUtils.hasLength(type)) {
            SysDictType sysDictType = sysDictTypeService.queryByType(type);
            model.addAttribute("sysDictType",sysDictType);
        }

        if (id != null) {
            model.addAttribute("sysDictData",sysDictDataService.get(id));
        }
       return "/sysDictData/input";
    }

}
